<template>
	<view class="content">
		<view class="analysis">
			<textarea class="textarea" v-model="address" placeholder="粘贴地址信息，自动拆分姓名、地址和联系方式。例如：上海市上海市青浦区XX镇XX路XX号 张三 18612773738。识别后请仔细检查地址是都有误！"/>
			<view style="display: flex;justify-content: space-between;align-items: center;">
				<view></view>
				<view class="btns">
					<view class="btn" @click="parseBtn">智能解析</view>
				</view>
			</view>
		</view>
		<view class="inps">
			<view class="key">联系人</view>
			<view class="value">
				<input type="text" v-model="form.name" class="inp" placeholder="联系人姓名" :placeholder-style="{color:'#CCCCCC'}"/>
			</view>
		</view>
		<view class="inps">
			<view class="key">联系电话</view>
			<view class="value">
				<input type="text" v-model="form.phone" class="inp" placeholder="联系人电话" :placeholder-style="{color:'#CCCCCC'}"/>
			</view>
		</view>
		<view class="inps">
			<view class="key">选择地区</view>
			<view class="value">
				<liDDPicker @change="change" :level="3">{{txt}}</liDDPicker>
				<!-- <input type="text" v-model="form.phone" class="inp" placeholder="请选择地址" :placeholder-style="{color:'#CCCCCC'}"/> -->
				<u-icon name="arrow-right" style="margin-left: 20rpx;"></u-icon>
			</view>
		</view>
		<!-- <view class="inps">
			<view class="key">街道/乡镇</view>
			<view class="value">
				<input type="text" class="inp" placeholder="街道名称" :placeholder-style="{color:'#CCCCCC'}"/>
			</view>
		</view> -->
		<view class="inpss">
			<view class="key">详细地址</view>
			<view class="value">
				<textarea class="inp" v-model="form.detail" placeholder="请填写详细地址" :placeholder-style="{color:'#CCCCCC'}"/>
			</view>
		</view>
		<view class="btn" @click="enter">确定添加</view>
	</view>
</template>

<script>
	import liDDPicker from '../../components/liDD-picker/liDD-picker.vue'
	import {addressAdd,addressEdit,addressInfo,addressParse} from '../../common/api.js'
	export default {
		components:{liDDPicker},
		data() {
			return {
				txt:'省、市、区',
				status:'add',
				form:{
					send:'',
					name:'',
					phone:'',
					province:'',
					city:'',
					district:'',
					detail:''
				},
				address:''
			}
		},
		onLoad(e) {
			
			this.form.send = Boolean(Number(e.type))
			console.log(this.form.send)
			if(e.status){
				this.status = e.status
				if(e.status=='edit'){
					this.getInfo(e.id)
				}
			}
			
		},
		methods: {
			parseBtn(){
				addressParse({
					address:this.address
				}).then(res=>{
					console.log(res)
					if(res.code==10000){
						this.txt = res.data.province + '-' + res.data.city + '-' + res.data.district
						this.form.name = res.data.name
						this.form.phone = res.data.phone
						this.form.province = res.data.province
						this.form.city = res.data.city
						this.form.district = res.data.district
						this.form.detail = res.data.detail
					}
				})
			},
			getInfo(id){
				addressInfo({id}).then(res=>{
					console.log(res)
					if(res.code==10000){
						this.form = res.data
						this.txt = res.data.province + '-' + res.data.city + '-' + res.data.district
					}
				})
			},
			enter(){
				console.log(this.form)
				if(this.status=='add'){
					addressAdd(this.form).then(res=>{
						if(res.code==10000){
							uni.showToast({
								title: '新增成功',
								duration: 2000,
								icon:"none"
							});
							setTimeout(()=>{
								uni.navigateBack({
									delta: 1
								});
								
							},500)
						}
					})
				}else if(this.status=='edit'){
					addressEdit(this.form).then(res=>{
						if(res.code==10000){
							uni.showToast({
								title: '修改成功',
								duration: 2000,
								icon:"none"
							});
							setTimeout(()=>{
								uni.navigateBack({
									delta: 1
								});
								
							},500)
						}
					})
				}
			},
			change(e){
				console.log(e)
				this.form.province=e.data[0].name
				this.form.city=e.data[1].name
				this.form.district=e.data[2].name
				this.txt = e.data[0].name + '-' + e.data[1].name + '-' + e.data[2].name
			}
		}
	}
</script>
<style>
	page{
		background: #EDEFF2;
	}
</style>
<style lang="scss" scoped>
.content{
	width: 750rpx;
	padding: 32rpx;
	box-sizing: border-box;
	.analysis{
		width: 100%;
		// height: 333rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 38rpx 32rpx;
		box-sizing: border-box;
		.textarea{
			font-size: 28rpx;
			height: 200rpx;
		}
		.btns{
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			.btn{
				margin-left: 20rpx;
				width: 164rpx;
				height: 56rpx;
				background: #FFFFFF;
				border-radius: 30rpx;
				border: 1rpx solid #1AA261;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #1AA261;
				line-height: 56rpx;
				text-align: center;
				font-style: normal;
			}
		}
	}
	.inpss{
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx 40rpx;
		box-sizing: border-box;
		margin-top: 24rpx;
		.key{
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #666666;
			line-height: 28rpx;
			text-align: left;
			font-style: normal;
		}
		.value{
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			margin-top: 20rpx;
			.inp{
				font-size: 28rpx;
				text-align: left;
			}
		}
	}
	.inps{
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx 40rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 24rpx;
		.key{
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #666666;
			line-height: 28rpx;
			text-align: left;
			font-style: normal;
		}
		.value{
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			.inp{
				font-size: 28rpx;
				text-align: right;
			}
		}
	}
	.btn{
		width: 100%;
		height: 88rpx;
		background: #1AA261;
		border-radius: 44rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 30rpx;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
		font-style: normal;
		margin-top: 80rpx;
	}
}
</style>
